package com.lanou.dllo.vc.activity;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.fragment.AddressFragment;
import com.lanou.dllo.vc.fragment.SettingFragment;
import com.lanou.dllo.vc.fragment.NewsFragment;

public class MainActivity extends FragmentActivity {
    public TabHost tabhost;
    private FragmentManager fm;

    @Override
    protected void onCreate(@Nullable Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

        setContentView(R.layout.activity_mian);
        //隐藏软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        fm = getSupportFragmentManager();

        initTabhost();//初始化Tabhost
        init();// 用fragment替换占位
    }

    private void init() {
        // TODO Auto-generated method stub
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.newsView, new NewsFragment());// 用第一页内容的碎片替换第一个占位
        ft.replace(R.id.addressView, new AddressFragment());// 用第二页内容的碎片替换第二个占位
        ft.replace(R.id.installView, new SettingFragment());// 用第三页内容的碎片替换第三个占位


        ft.commit();
    }

    private void initTabhost() {

        tabhost = (TabHost) findViewById(android.R.id.tabhost);// 绑定tabhost

        tabhost.setup();

        TabSpec tabspec1 = tabhost.newTabSpec("tabspec1");

        View view1 = getLayoutInflater().inflate(R.layout.tab_news_item,
                null);// 找到标签
        tabspec1.setIndicator(view1);// 绑定标签

        tabspec1.setContent(R.id.newsView);// 绑定内容(firstView是个占位)

        tabhost.addTab(tabspec1);// 将绑定了标签和内容的tabspec添加到tabhost中

        TabSpec tabspec2 = tabhost.newTabSpec("tabspec2");

        View view2 = getLayoutInflater().inflate(
                R.layout.tab_address_item, null);
        tabspec2.setIndicator(view2);
        tabspec2.setContent(R.id.addressView);// 绑定内容(secondtView是个占位)

        tabhost.addTab(tabspec2);// 将绑定了标签和内容的tabspec添加到tabhost中

        TabSpec tabspec3 = tabhost.newTabSpec("tabspec3");

        View view3 = getLayoutInflater().inflate(R.layout.tab_install_item,
                null);
        tabspec3.setIndicator(view3);
        tabspec3.setContent(R.id.installView);// 绑定内容(thirdView是个占位)

        tabhost.addTab(tabspec3);// 将绑定了标签和内容的tabspec添加到tabhost中


    }

}
